from django.db import models
from django.db import models
from django.contrib.auth.hashers import make_password

class Users(models.Model):
    """
    用户表
    """
    id = models.AutoField(primary_key=True)  # 用户ID
    username = models.CharField(max_length=255, unique=True)  # 用户名
    email = models.EmailField(max_length=255, unique=True)  # 邮箱
    password = models.CharField(max_length=255)  # 密码（加密存储）
    created_at = models.DateTimeField(auto_now_add=True)  # 注册时间
    updated_at = models.DateTimeField(auto_now=True)  # 更新时间

    def save(self, *args, **kwargs):
        if not self.pk:  # 如果是新用户，密码进行加密
            self.password = make_password(self.password)
        super().save(*args, **kwargs)

    def __str__(self):
        return self.username